#include "pagepisskipstationselect.h"
#include "ui_pagepisskipstationselect.h"


PagePisSkipStationSelect::PagePisSkipStationSelect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PagePisSkipStationSelect)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_nCurLineID = 0;
    ui->RouteName->setText(QString("%1     开往     %2").arg(GlobalConfig::stationList[m_nCurLineID].startName).arg(GlobalConfig::stationList[m_nCurLineID].destName));
    InitSkipStationBtns();
    setPageActive(false);
}

PagePisSkipStationSelect::~PagePisSkipStationSelect()
{
    delete ui;
}

void PagePisSkipStationSelect::setPageActive(bool xActive)
{
    ui->Btn_PisSkipStationClear->setEnabled(xActive);
    ui->Btn_PisSkipStationSet->setEnabled(xActive);

    for(int i=0;i<m_lSkipStationBtnList.length();i++)
        m_lSkipStationBtnList[i]->setEnabled(xActive);
}

void PagePisSkipStationSelect::InitSkipStationBtns()
{
    m_nColsInRow = 5;
    total_station_cnt_ = GlobalConfig::stationList[m_nCurLineID].stationCount;
    m_nStartx = ui->Label_Line->pos().x();
    m_nStarty = ui->Label_Line->pos().y() + 70;
    uint nAreaWidth = ui->frame->width()-2*ui->Label_Line->pos().x();
    m_nStationLabelLen  = 140;
    m_nStationLabelHeight = 50;
    m_nHSpace = (nAreaWidth - m_nColsInRow*m_nStationLabelLen)/(m_nColsInRow-1);
    m_nVSpace = 10;

    for(int i=0;i<total_station_cnt_;i++)
    {
        QPushButton *stationBtn = new QPushButton(ui->frame);
        stationBtn->setStyleSheet(StationBtnStyle);
        stationBtn->setGeometry(m_nStartx+(i%m_nColsInRow)*(m_nStationLabelLen+m_nHSpace),
                                m_nStarty+(i/m_nColsInRow)*(m_nStationLabelHeight+m_nVSpace),
                                m_nStationLabelLen,m_nStationLabelHeight);
        stationBtn->setText(GlobalConfig::stationList[m_nCurLineID].station.at(i).name);
        stationBtn->setCheckable(true);
        m_lSkipStationBtnList.append(stationBtn);
    }
}

void PagePisSkipStationSelect::ChangeLineInfo(uint8_t id)
{
    if(m_nCurLineID == id-1)
        return;
    m_nCurLineID = id-1;
    ui->RouteName->setText(QString("%1     开往     %2").arg(GlobalConfig::stationList[m_nCurLineID].startName).arg(GlobalConfig::stationList[m_nCurLineID].destName));
    int btn_checked_flag = 0;
    for(int i=0;i<m_lSkipStationBtnList.length();i++)
    {
        if(m_lSkipStationBtnList[i]->isChecked())
        {
            m_lSkipStationBtnList[i]->setChecked(false);
            btn_checked_flag  |= 0x00000001 << i;
        }
        m_lSkipStationBtnList[i]->setText(GlobalConfig::stationList[m_nCurLineID].station.at(i).name);
    }
    for(int i=0;i<m_lSkipStationBtnList.length();i++)
    {
        if(btn_checked_flag & (0x00000001 << i))
            m_lSkipStationBtnList[m_lSkipStationBtnList.length()-1-i]->setChecked(true);
    }
    ReMarkSkipStationBtns(skip_stations_id_);
}

void PagePisSkipStationSelect::on_Btn_PisSkipStationClear_clicked()
{
    for(int i=0;i<m_lSkipStationBtnList.length();i++)
    {
        m_lSkipStationBtnList[i]->setStyleSheet(StationBtnStyle);
        m_lSkipStationBtnList[i]->setChecked(false);
    }
    skip_stations_id_ = 0;
    emit signal_SkipStationID(skip_stations_id_);
}

void PagePisSkipStationSelect::SyncFeedBackSkipStations(uint16_t value)
{
    skip_stations_id_ = value;
    ReMarkSkipStationBtns(skip_stations_id_);
    emit signal_SkipStationID(skip_stations_id_);
}

void PagePisSkipStationSelect::ReMarkSkipStationBtns(uint32_t skip_id)
{
    for (int i=0;i<m_lSkipStationBtnList.length() ;++i ) {
        if(skip_id & (0x0001 << i))
        {
            if(m_nCurLineID == 0)
                m_lSkipStationBtnList[i]->setStyleSheet(StationBtnYellowStyle);
            else if(m_nCurLineID == 1)
                m_lSkipStationBtnList[m_lSkipStationBtnList.length()-1-i]->setStyleSheet(StationBtnYellowStyle);
        }
        else
        {
            if(m_nCurLineID == 0)
                m_lSkipStationBtnList[i]->setStyleSheet(StationBtnStyle);
            else if(m_nCurLineID == 1)
                m_lSkipStationBtnList[m_lSkipStationBtnList.length()-1-i]->setStyleSheet(StationBtnStyle);
        }
    }
}

void PagePisSkipStationSelect::on_Btn_PisSkipStationSet_clicked()
{
    for (int i=0; i<m_lSkipStationBtnList.length();++i ) {
        if(m_lSkipStationBtnList.at(i)->isChecked())
        {
            int transformed_id = i;
            if(m_nCurLineID == 1)
                transformed_id = total_station_cnt_ - i - 1;
            skip_stations_id_ |= (0x0001 << transformed_id);
        }
    }
    emit signal_SkipStationID(skip_stations_id_);

    emit signal_SkipStationActive(true);
    ui->Btn_PisSkipStationClear->setEnabled(false);
    ui->Btn_PisSkipStationSet->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_SkipStationActive(false);
        ui->Btn_PisSkipStationClear->setEnabled(true);
        ui->Btn_PisSkipStationSet->setEnabled(true);
    });
}
